import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/modules/user'
import { routes } from '@/router/routes'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 登录访问拦截
router.beforeEach((to) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  // 判断页面是否需要登录验证
  if (to.meta.requiresAuth) {
    const userStore = useUserStore()
    if (!userStore.token) {
      // 如果当前用户未登录，跳转到登录页面
      return '/login'
    }
  }
})

export default router
